﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractMainTask;
using HamcoWcs.Contract.ContractMes;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.MainTaskBaks;
using HamcoWcs.Entitys.MainTasks;
using HamcoWcs.Entitys.SubTasks;
using HamcoWcs.Entitys.WcsLocations;
using HamcoWcs.Models.Common;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using HamcoWcs.Models.ForeEndModels.UITasks;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;

namespace HamcoWcs.Service.Srm.SrmTaskParsers
{
    /// <summary>空取货</summary>
    public class SrmParser_EmptyPick : SrmParserBase
    {
        public override EnumSrmRequest RequestType => EnumSrmRequest.EmptyPick;

        public override void Deal(SrmDto srm, SrmPlc srmPlc)
        {
            var task = GetSrmTask(srm);
            if (task is null) return;

            _logger.AddWcsRunLog(task.UnitId, $"{srm.DeviceName}上报空取货");
            task.MainTask.TaskStatus = EnumMainTaskStatus.Cancel.ToString();
            _logger.AddWcsRunLog(task.UnitId, $"{task.MainTask.TaskId}任务状态置为Cancel");

            var loc = _locationService.GetLocationByWcsName(task.FromLoc);
            if (loc is null) return;
            
            loc.IsLock = true;
            loc.Remark = $"[{task.MainTask.TaskId}]执行时发生空取货，锁定货架";
            _wcsDbContext.SaveChanges();
        }



        private readonly IBaseMainTaskService _mainTaskService;
        private readonly IMapper _mapper;
        public SrmParser_EmptyPick(IMapper mapper, IBaseMainTaskService mainTaskService, ILocationService locationService, WcsDbContext wcsDbContext,
            IMesCommunicateService mesCommunicateService, ILoggerService logger)
            : base(wcsDbContext, locationService, mesCommunicateService, logger)
        {
            _mainTaskService = mainTaskService;
            _mapper = mapper;
        }


    }
}
